package hsrt.vis;

import android.R.string;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class Vis3D extends Activity {
	private LinearLayout root;
	private EditText tb;
	SphereView mSphereView;
	Button button_right,button_left;
	EditText text1;
    MotionEvent touch_kugel;
	View custom_view;
	SphereRenderer sr;
	double touch1y=0.0;
	double touch2y=0.0;
	double touch1x=0.0;
	double touch2x=0.0;
	Boolean firstTouch=true;
	private static final float ANGLE_DIFF = 4.0f;
	private int selectedArea=1;

	
//	private static final String TAG = "MyActivity";
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        /* Create custom view */
        LinearLayout.LayoutParams containerParams
        = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            0.0F);
        
        LinearLayout.LayoutParams widgetParams
        = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT,
            1.0F);
        
        root = new LinearLayout(this);
        root.setOrientation(LinearLayout.VERTICAL);
        root.setBackgroundColor(Color.WHITE);
        root.setLayoutParams(containerParams);
        
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setBackgroundColor(Color.GRAY);
        ll.setLayoutParams(containerParams);
        root.addView(ll);
        
        this.tb = new EditText(this);
        setTbText("CallStatus");
        tb.setTextSize(12);
        tb.setBackgroundColor(000000);
        tb.setHeight(50);
        tb.setFocusable(false);
        tb.setLayoutParams(widgetParams);
        tb.setClickable(true);
        tb.setPadding(0, 0, 0, 0);
        tb.setOnClickListener(new click_listener());
        ll.addView(tb);
        
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setBackgroundColor(Color.DKGRAY);
        ll.setLayoutParams(containerParams);
        root.addView(ll);
        
        /* Create Sphere View */
        mSphereView = new SphereView(this);
        sr=new SphereRenderer(this);
        mSphereView.setRenderer(sr);
        mSphereView.setOnTouchListener(new touch_listener());
        ll.addView(mSphereView);
        
        /* Set customView */
        setContentView(root);
    }
    
    /* This function updates the textfield */ 
    public void setTbText(String dataText) {
    	this.tb.setText(dataText);
    	this.tb.setText(this.tb.getText());
    }
    
    /* This function sets the height of the textfield */
    public void setTbHeight(int height) {
    	this.tb.setHeight(height);
    }
    
    /* Get height of textfield */
    public int getTbHeight() {
    	return this.tb.getHeight();
    }
    
    public void onDestroy() {
    	Debug.stopMethodTracing();
    	super.onDestroy();
    }
    
    /* This function checks if the current visible Part of the Sphere has been changed */
    public void checkSelectedPart() {
    	int selPart=sr.getSelArea();
    	if (selPart!=this.selectedArea) {
    		setTbText(sr.getSelectedDataText());
    		this.selectedArea=selPart;
    	}
    }
    
    /* Function for rotation per keys */
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent msg) { 
    	if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) { 
          sr.rotateY(-ANGLE_DIFF); 
          checkSelectedPart();
            return true; 
        } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) { 
          sr.rotateY(ANGLE_DIFF); 
          checkSelectedPart();
            return true; 
        } 
        return false; 
    } 
    
    /* clickListener Class for textfield */
    class click_listener implements OnClickListener
    {
    	public void onClick(View v) {
    		if (getTbHeight()>50) {
    			setTbHeight(50);
    		} else {
    			setTbHeight(100);
    		}
    		
    	}
    }
    
    /* touchListener Class which register display touches */
    class touch_listener implements OnTouchListener
    {
		public boolean onTouch(View v,MotionEvent me)
    	{	
			if (firstTouch) {
				touch1x=me.getRawX();
				firstTouch=false;
				if (touch2x!=0.0) {				
					if (touch1x<touch2x) {
						sr.rotateY(-ANGLE_DIFF); 
						checkSelectedPart();
					} else {						
						sr.rotateY(ANGLE_DIFF); 
						checkSelectedPart();						
					}
				} else {
					//Do nothing
				}
			} else {
				touch2x=me.getRawX();
				firstTouch=true;
				 if (touch1x!=0.0) {
					if (touch2x<touch1x) {
						sr.rotateY(-ANGLE_DIFF); 
						checkSelectedPart();
					} else {			
						sr.rotateY(ANGLE_DIFF); 
						checkSelectedPart();						
					}
				} else {
					//Do nothing
				} 
			} 
			return true;	
    	}
		
    }
    
}